home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsdevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.6 KB  |  815 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsdevice.c,v 1.7 2000/09/19 19:00:27 lpd Exp $ */
  20. /* Device operators for Ghostscript library */
  21. #include "ctype_.h"
  22. #include "memory_.h"        /* for memchr, memcpy */
  23. #include "string_.h"
  24. #include "gx.h"
  25. #include "gp.h"
  26. #include "gscdefs.h"        /* for gs_lib_device_list */
  27. #include "gserrors.h"
  28. #include "gsfname.h"
  29. #include "gsstruct.h"
  30. #include "gspath.h"        /* gs_initclip prototype */
  31. #include "gspaint.h"        /* gs_erasepage prototype */
  32. #include "gsmatrix.h"        /* for gscoord.h */
  33. #include "gscoord.h"        /* for gs_initmatrix */
  34. #include "gzstate.h"
  35. #include "gxcmap.h"
  36. #include "gxdevice.h"
  37. #include "gxdevmem.h"
  38. #include "gxiodev.h"
  39.  
  40. /* Include the extern for the device list. */
  41. extern_gs_lib_device_list();
  42.  
  43. /*
  44.  * Finalization for devices: do any special finalization first, then
  45.  * close the device if it is open, and finally free the structure
  46.  * descriptor if it is dynamic.
  47.  */
  48. void
  49. gx_device_finalize(void *vptr)
  50. {
  51.     gx_device * const dev = (gx_device *)vptr;
  52.  
  53.     if (dev->finalize)
  54.     dev->finalize(dev);
  55.     discard(gs_closedevice(dev));
  56.     if (dev->stype_is_dynamic)
  57.     gs_free_const_object(&gs_memory_default, dev->stype,
  58.                  "gx_device_finalize");
  59. }
  60.  
  61. /* "Free" a device locally allocated on the stack, by finalizing it. */
  62. void
  63. gx_device_free_local(gx_device *dev)
  64. {
  65.     gx_device_finalize(dev);
  66. }
  67.  
  68. /* GC procedures */
  69. private 
  70. ENUM_PTRS_WITH(device_forward_enum_ptrs, gx_device_forward *fdev) return 0;
  71. case 0: ENUM_RETURN(gx_device_enum_ptr(fdev->target));
  72. ENUM_PTRS_END
  73. private RELOC_PTRS_WITH(device_forward_reloc_ptrs, gx_device_forward *fdev)
  74. {
  75.     fdev->target = gx_device_reloc_ptr(fdev->target, gcst);
  76. }
  77. RELOC_PTRS_END
  78.  
  79. /*
  80.  * Structure descriptors.  These must follow the procedures, because
  81.  * we can't conveniently forward-declare the procedures.
  82.  * (See gxdevice.h for details.)
  83.  */
  84. public_st_device();
  85. public_st_device_forward();
  86. public_st_device_null();
  87.  
  88. /* GC utilities */
  89. /* Enumerate or relocate a device pointer for a client. */
  90. gx_device *
  91. gx_device_enum_ptr(gx_device * dev)
  92. {
  93.     if (dev == 0 || dev->memory == 0)
  94.     return 0;
  95.     return dev;
  96. }
  97. gx_device *
  98. gx_device_reloc_ptr(gx_device * dev, gc_state_t * gcst)
  99. {
  100.     if (dev == 0 || dev->memory == 0)
  101.     return dev;
  102.     return RELOC_OBJ(dev);    /* gcst implicit */
  103. }
  104.  
  105. /* Set up the device procedures in the device structure. */
  106. /* Also copy old fields to new ones. */
  107. void
  108. gx_device_set_procs(gx_device * dev)
  109. {
  110.     if (dev->static_procs != 0) {    /* 0 if already populated */
  111.     dev->procs = *dev->static_procs;
  112.     dev->static_procs = 0;
  113.     }
  114. }
  115.  
  116. /* Flush buffered output to the device */
  117. int
  118. gs_flushpage(gs_state * pgs)
  119. {
  120.     gx_device *dev = gs_currentdevice(pgs);
  121.  
  122.     return (*dev_proc(dev, sync_output)) (dev);
  123. }
  124.  
  125. /* Make the device output the accumulated page description */
  126. int
  127. gs_copypage(gs_state * pgs)
  128. {
  129.     return gs_output_page(pgs, 1, 0);
  130. }
  131. int
  132. gs_output_page(gs_state * pgs, int num_copies, int flush)
  133. {
  134.     gx_device *dev = gs_currentdevice(pgs);
  135.  
  136.     if (dev->IgnoreNumCopies)
  137.     num_copies = 1;
  138.     return (*dev_proc(dev, output_page)) (dev, num_copies, flush);
  139. }
  140.  
  141. /*
  142.  * Do generic work for output_page.  All output_page procedures must call
  143.  * this as the last thing they do, unless an error has occurred earlier.
  144.  */
  145. int
  146. gx_finish_output_page(gx_device *dev, int num_copies, int flush)
  147. {
  148.     dev->PageCount += num_copies;
  149.     return 0;
  150. }
  151.  
  152. /* Copy scan lines from an image device */
  153. int
  154. gs_copyscanlines(gx_device * dev, int start_y, byte * data, uint size,
  155.          int *plines_copied, uint * pbytes_copied)
  156. {
  157.     uint line_size = gx_device_raster(dev, 0);
  158.     uint count = size / line_size;
  159.     uint i;
  160.     byte *dest = data;
  161.  
  162.     for (i = 0; i < count; i++, dest += line_size) {
  163.     int code = (*dev_proc(dev, get_bits)) (dev, start_y + i, dest, NULL);
  164.  
  165.     if (code < 0) {
  166.         /* Might just be an overrun. */
  167.         if (start_y + i == dev->height)
  168.         break;
  169.         return_error(code);
  170.     }
  171.     }
  172.     if (plines_copied != NULL)
  173.     *plines_copied = i;
  174.     if (pbytes_copied != NULL)
  175.     *pbytes_copied = i * line_size;
  176.     return 0;
  177. }
  178.  
  179. /* Get the current device from the graphics state. */
  180. gx_device *
  181. gs_currentdevice(const gs_state * pgs)
  182. {
  183.     return pgs->device;
  184. }
  185.  
  186. /* Get the name of a device. */
  187. const char *
  188. gs_devicename(const gx_device * dev)
  189. {
  190.     return dev->dname;
  191. }
  192.  
  193. /* Get the initial matrix of a device. */
  194. void
  195. gs_deviceinitialmatrix(gx_device * dev, gs_matrix * pmat)
  196. {
  197.     fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  198.     (*dev_proc(dev, get_initial_matrix)) (dev, pmat);
  199. }
  200.  
  201. /* Get the N'th device from the known device list */
  202. const gx_device *
  203. gs_getdevice(int index)
  204. {
  205.     const gx_device *const *list;
  206.     int count = gs_lib_device_list(&list, NULL);
  207.  
  208.     if (index < 0 || index >= count)
  209.     return 0;        /* index out of range */
  210.     return list[index];
  211. }
  212.  
  213. /* Fill in the GC structure descriptor for a device. */
  214. private void
  215. gx_device_make_struct_type(gs_memory_struct_type_t *st,
  216.                const gx_device *dev)
  217. {
  218.     const gx_device_procs *procs = dev->static_procs;
  219.  
  220.     /*
  221.      * Try to figure out whether this is a forwarding device.  For printer
  222.      * devices, we rely on the prototype referencing the correct structure
  223.      * descriptor; for other devices, we look for a likely forwarding
  224.      * procedure in the vector.  The algorithm isn't foolproof, but it's the
  225.      * best we can come up with.
  226.      */
  227.     if (procs == 0)
  228.     procs = &dev->procs;
  229.     if (dev->stype)
  230.     *st = *dev->stype;
  231.     else if (procs->get_xfont_procs == gx_forward_get_xfont_procs)
  232.     *st = st_device_forward;
  233.     else
  234.     *st = st_device;
  235.     st->ssize = dev->params_size;
  236. }
  237.  
  238. /* Clone an existing device. */
  239. int
  240. gs_copydevice2(gx_device ** pnew_dev, const gx_device * dev, bool keep_open,
  241.            gs_memory_t * mem)
  242. {
  243.     gx_device *new_dev;
  244.     const gs_memory_struct_type_t *std = dev->stype;
  245.     const gs_memory_struct_type_t *new_std;
  246.     gs_memory_struct_type_t *a_std = 0;
  247.     int code;
  248.  
  249.     if (dev->stype_is_dynamic) {
  250.     /*
  251.      * We allocated the stype for this device previously.
  252.      * Just allocate a new stype and copy the old one into it.
  253.      */
  254.     a_std = (gs_memory_struct_type_t *)
  255.         gs_alloc_bytes_immovable(&gs_memory_default, sizeof(*std),
  256.                      "gs_copydevice(stype)");
  257.     if (!a_std)
  258.         return_error(gs_error_VMerror);
  259.     *a_std = *std;
  260.     new_std = a_std;
  261.     } else if (std != 0 && std->ssize == dev->params_size) {
  262.     /* Use the static stype. */
  263.     new_std = std;
  264.     } else {
  265.     /* We need to figure out or adjust the stype. */
  266.     a_std = (gs_memory_struct_type_t *)
  267.         gs_alloc_bytes_immovable(&gs_memory_default, sizeof(*std),
  268.                      "gs_copydevice(stype)");
  269.     if (!a_std)
  270.         return_error(gs_error_VMerror);
  271.     gx_device_make_struct_type(a_std, dev);
  272.     new_std = a_std;
  273.     }
  274.     /*
  275.      * Because command list devices have complicated internal pointer
  276.      * structures, we allocate all device instances as immovable.
  277.      */
  278.     new_dev = gs_alloc_struct_immovable(mem, gx_device, new_std,
  279.                     "gs_copydevice(device)");
  280.     if (new_dev == 0)
  281.     return_error(gs_error_VMerror);
  282.     gx_device_init(new_dev, dev, mem, false);
  283.     new_dev->stype = new_std;
  284.     new_dev->stype_is_dynamic = new_std != std;
  285.     /*
  286.      * keep_open is very dangerous.  On the other hand, so is copydevice in
  287.      * general, since it just copies the bits without any regard to pointers
  288.      * (including self-pointers) that they may contain.  We handle this by
  289.      * making the default finish_copydevice forbid copying of anything other
  290.      * than the device prototype.
  291.      */
  292.     new_dev->is_open = dev->is_open && keep_open;
  293.     fill_dev_proc(new_dev, finish_copydevice, gx_default_finish_copydevice);
  294.     code = dev_proc(new_dev, finish_copydevice)(new_dev, dev);
  295.     if (code < 0) {
  296.     gs_free_object(mem, new_dev, "gs_copydevice(device)");
  297.     if (a_std)
  298.         gs_free_object(&gs_memory_default, a_std, "gs_copydevice(stype)");
  299.     return code;
  300.     }
  301.     *pnew_dev = new_dev;
  302.     return 0;
  303. }
  304. int
  305. gs_copydevice(gx_device ** pnew_dev, const gx_device * dev, gs_memory_t * mem)
  306. {
  307.     return gs_copydevice2(pnew_dev, dev, false, mem);
  308. }
  309.  
  310. /* Open a device if not open already.  Return 0 if the device was open, */
  311. /* 1 if it was closed. */
  312. int
  313. gs_opendevice(gx_device *dev)
  314. {
  315.     if (dev->is_open)
  316.     return 0;
  317.     gx_device_fill_in_procs(dev);
  318.     {
  319.     int code = (*dev_proc(dev, open_device))(dev);
  320.  
  321.     if (code < 0)
  322.         return_error(code);
  323.     dev->is_open = true;
  324.     return 1;
  325.     }
  326. }
  327.  
  328. /* Set device parameters, updating a graphics state or imager state. */
  329. int
  330. gs_imager_putdeviceparams(gs_imager_state *pis, gx_device *dev,
  331.               gs_param_list *plist)
  332. {
  333.     int code = gs_putdeviceparams(dev, plist);
  334.  
  335.     if (code >= 0)
  336.     gx_set_cmap_procs(pis, dev);
  337.     return code;
  338. }
  339. private void
  340. gs_state_update_device(gs_state *pgs)
  341. {
  342.     gx_set_cmap_procs((gs_imager_state *)pgs, pgs->device);
  343.     gx_unset_dev_color(pgs);
  344. }
  345. int
  346. gs_state_putdeviceparams(gs_state *pgs, gs_param_list *plist)
  347. {
  348.     int code = gs_putdeviceparams(pgs->device, plist);
  349.  
  350.     if (code >= 0)
  351.     gs_state_update_device(pgs);
  352.     return code;
  353. }
  354.  
  355. /* Set the device in the graphics state */
  356. int
  357. gs_setdevice(gs_state * pgs, gx_device * dev)
  358. {
  359.     int code = gs_setdevice_no_erase(pgs, dev);
  360.  
  361.     if (code == 1)
  362.     code = gs_erasepage(pgs);
  363.     return code;
  364. }
  365. int
  366. gs_setdevice_no_erase(gs_state * pgs, gx_device * dev)
  367. {
  368.     int open_code = 0, code;
  369.  
  370.     /* Initialize the device */
  371.     if (!dev->is_open) {
  372.     gx_device_fill_in_procs(dev);
  373.     if (gs_device_is_memory(dev)) {
  374.         /* Set the target to the current device. */
  375.         gx_device *odev = gs_currentdevice_inline(pgs);
  376.  
  377.         while (odev != 0 && gs_device_is_memory(odev))
  378.         odev = ((gx_device_memory *)odev)->target;
  379.         gx_device_set_target(((gx_device_forward *)dev), odev);
  380.     }
  381.     code = open_code = gs_opendevice(dev);
  382.     if (code < 0)
  383.         return code;
  384.     }
  385.     gs_setdevice_no_init(pgs, dev);
  386.     pgs->ctm_default_set = false;
  387.     if ((code = gs_initmatrix(pgs)) < 0 ||
  388.     (code = gs_initclip(pgs)) < 0
  389.     )
  390.     return code;
  391.     /* If we were in a charpath or a setcachedevice, */
  392.     /* we aren't any longer. */
  393.     pgs->in_cachedevice = 0;
  394.     pgs->in_charpath = (gs_char_path_mode) 0;
  395.     return open_code;
  396. }
  397. int
  398. gs_setdevice_no_init(gs_state * pgs, gx_device * dev)
  399. {
  400.     /*
  401.      * Just set the device, possibly changing color space but no other
  402.      * device parameters.
  403.      */
  404.     rc_assign(pgs->device, dev, "gs_setdevice_no_init");
  405.     gs_state_update_device(pgs);
  406.     return 0;
  407. }
  408.  
  409. /* Initialize a just-allocated device. */
  410. void
  411. gx_device_init(gx_device * dev, const gx_device * proto, gs_memory_t * mem,
  412.            bool internal)
  413. {
  414.     memcpy(dev, proto, proto->params_size);
  415.     dev->memory = mem;
  416.     dev->retained = !internal;
  417.     rc_init(dev, mem, (internal ? 0 : 1));
  418. }
  419.  
  420. /* Make a null device. */
  421. void
  422. gs_make_null_device(gx_device_null *dev_null, gx_device *dev,
  423.             gs_memory_t * mem)
  424. {
  425.     gx_device_init((gx_device *)dev_null, (const gx_device *)&gs_null_device,
  426.            mem, true);
  427.     gx_device_set_target((gx_device_forward *)dev_null, dev);
  428.     if (dev)
  429.     gx_device_copy_color_params((gx_device *)dev_null, dev);
  430. }
  431.  
  432. /* Mark a device as retained or not retained. */
  433. void
  434. gx_device_retain(gx_device *dev, bool retained)
  435. {
  436.     int delta = (int)retained - (int)dev->retained;
  437.  
  438.     if (delta) {
  439.     dev->retained = retained; /* do first in case dev is freed */
  440.     rc_adjust_only(dev, delta, "gx_device_retain");
  441.     }
  442. }
  443.  
  444. /* Select a null device. */
  445. int
  446. gs_nulldevice(gs_state * pgs)
  447. {
  448.     if (pgs->device == 0 || !gx_device_is_null(pgs->device)) {
  449.     gx_device *ndev;
  450.     int code = gs_copydevice(&ndev, (const gx_device *)&gs_null_device,
  451.                  pgs->memory);
  452.  
  453.     if (code < 0)
  454.         return code;
  455.     /*
  456.      * Internal devices have a reference count of 0, not 1,
  457.      * aside from references from graphics states.
  458.      */
  459.     rc_init(ndev, pgs->memory, 0);
  460.     return gs_setdevice_no_erase(pgs, ndev);
  461.     }
  462.     return 0;
  463. }
  464.  
  465. /* Close a device.  The client is responsible for ensuring that */
  466. /* this device is not current in any graphics state. */
  467. int
  468. gs_closedevice(gx_device * dev)
  469. {
  470.     int code = 0;
  471.  
  472.     if (dev->is_open) {
  473.     code = (*dev_proc(dev, close_device))(dev);
  474.     if (code < 0)
  475.         return_error(code);
  476.     dev->is_open = false;
  477.     }
  478.     return code;
  479. }
  480.  
  481. /*
  482.  * Just set the device without any reinitializing.
  483.  * (For internal use only.)
  484.  */
  485. void
  486. gx_set_device_only(gs_state * pgs, gx_device * dev)
  487. {
  488.     rc_assign(pgs->device, dev, "gx_set_device_only");
  489. }
  490.  
  491. /* Compute the size of one scan line for a device, */
  492. /* with or without padding to a word boundary. */
  493. uint
  494. gx_device_raster(const gx_device * dev, bool pad)
  495. {
  496.     ulong bits = (ulong) dev->width * dev->color_info.depth;
  497.  
  498.     return (pad ? bitmap_raster(bits) : (uint) ((bits + 7) >> 3));
  499. }
  500.  
  501. /* Adjust the resolution for devices that only have a fixed set of */
  502. /* geometries, so that the apparent size in inches remains constant. */
  503. /* If fit=1, the resolution is adjusted so that the entire image fits; */
  504. /* if fit=0, one dimension fits, but the other one is clipped. */
  505. int
  506. gx_device_adjust_resolution(gx_device * dev,
  507.                 int actual_width, int actual_height, int fit)
  508. {
  509.     double width_ratio = (double)actual_width / dev->width;
  510.     double height_ratio = (double)actual_height / dev->height;
  511.     double ratio =
  512.     (fit ? min(width_ratio, height_ratio) :
  513.      max(width_ratio, height_ratio));
  514.  
  515.     dev->HWResolution[0] *= ratio;
  516.     dev->HWResolution[1] *= ratio;
  517.     gx_device_set_width_height(dev, actual_width, actual_height);
  518.     return 0;
  519. }
  520.  
  521. /* Set the HWMargins to values defined in inches. */
  522. /* If move_origin is true, also reset the Margins. */
  523. /* Note that this assumes a printer-type device (Y axis inverted). */
  524. void
  525. gx_device_set_margins(gx_device * dev, const float *margins /*[4] */ ,
  526.               bool move_origin)
  527. {
  528.     int i;
  529.  
  530.     for (i = 0; i < 4; ++i)
  531.     dev->HWMargins[i] = margins[i] * 72.0;
  532.     if (move_origin) {
  533.     dev->Margins[0] = -margins[0] * dev->MarginsHWResolution[0];
  534.     dev->Margins[1] = -margins[3] * dev->MarginsHWResolution[1];
  535.     }
  536. }
  537.  
  538. /* Set the width and height, updating MediaSize to remain consistent. */
  539. void
  540. gx_device_set_width_height(gx_device * dev, int width, int height)
  541. {
  542.     dev->width = width;
  543.     dev->height = height;
  544.     dev->MediaSize[0] = width * 72.0 / dev->HWResolution[0];
  545.     dev->MediaSize[1] = height * 72.0 / dev->HWResolution[1];
  546. }
  547.  
  548. /* Set the resolution, updating width and height to remain consistent. */
  549. void
  550. gx_device_set_resolution(gx_device * dev, floatp x_dpi, floatp y_dpi)
  551. {
  552.     dev->HWResolution[0] = x_dpi;
  553.     dev->HWResolution[1] = y_dpi;
  554.     dev->width = dev->MediaSize[0] * x_dpi / 72.0 + 0.5;
  555.     dev->height = dev->MediaSize[1] * y_dpi / 72.0 + 0.5;
  556. }
  557.  
  558. /* Set the MediaSize, updating width and height to remain consistent. */
  559. void
  560. gx_device_set_media_size(gx_device * dev, floatp media_width, floatp media_height)
  561. {
  562.     dev->MediaSize[0] = media_width;
  563.     dev->MediaSize[1] = media_height;
  564.     dev->width = media_width * dev->HWResolution[0] / 72.0 + 0.499;
  565.     dev->height = media_height * dev->HWResolution[1] / 72.0 + 0.499;
  566. }
  567.  
  568. /*
  569.  * Copy the color mapping procedures from the target if they are
  570.  * standard ones (saving a level of procedure call at mapping time).
  571.  */
  572. void
  573. gx_device_copy_color_procs(gx_device *dev, const gx_device *target)
  574. {
  575.     dev_proc_map_cmyk_color((*from_cmyk)) =
  576.     dev_proc(dev, map_cmyk_color);
  577.     dev_proc_map_rgb_color((*from_rgb)) =
  578.     dev_proc(dev, map_rgb_color);
  579.     dev_proc_map_color_rgb((*to_rgb)) =
  580.     dev_proc(dev, map_color_rgb);
  581.  
  582.     if (from_cmyk == gx_forward_map_cmyk_color ||
  583.     from_cmyk == cmyk_1bit_map_cmyk_color ||
  584.     from_cmyk == cmyk_8bit_map_cmyk_color) {
  585.     from_cmyk = dev_proc(target, map_cmyk_color);
  586.     set_dev_proc(dev, map_cmyk_color,
  587.              (from_cmyk == cmyk_1bit_map_cmyk_color ||
  588.               from_cmyk == cmyk_8bit_map_cmyk_color ?
  589.               from_cmyk : gx_forward_map_cmyk_color));
  590.     }
  591.     if (from_rgb == gx_forward_map_rgb_color ||
  592.     from_rgb == gx_default_rgb_map_rgb_color) {
  593.     from_rgb = dev_proc(target, map_rgb_color);
  594.     set_dev_proc(dev, map_rgb_color,
  595.              (from_rgb == gx_default_rgb_map_rgb_color ?
  596.               from_rgb : gx_forward_map_rgb_color));
  597.     }
  598.     if (to_rgb == gx_forward_map_color_rgb ||
  599.     to_rgb == cmyk_1bit_map_color_rgb ||
  600.     to_rgb == cmyk_8bit_map_color_rgb) {
  601.     to_rgb = dev_proc(target, map_color_rgb);
  602.     set_dev_proc(dev, map_color_rgb,
  603.              (to_rgb == cmyk_1bit_map_color_rgb ||
  604.               to_rgb == cmyk_8bit_map_color_rgb ?
  605.               to_rgb : gx_forward_map_color_rgb));
  606.     }
  607. }
  608.  
  609. #define COPY_PARAM(p) dev->p = target->p
  610.  
  611. /*
  612.  * Copy the color-related device parameters back from the target:
  613.  * color_info and color mapping procedures.
  614.  */
  615. void
  616. gx_device_copy_color_params(gx_device *dev, const gx_device *target)
  617. {
  618.     COPY_PARAM(color_info);
  619.     COPY_PARAM(cached_colors);
  620.     gx_device_copy_color_procs(dev, target);
  621. }
  622.  
  623. /*
  624.  * Copy device parameters back from a target.  This copies all standard
  625.  * parameters related to page size and resolution, plus color_info
  626.  * and (if appropriate) color mapping procedures.
  627.  */
  628. void
  629. gx_device_copy_params(gx_device *dev, const gx_device *target)
  630. {
  631. #define COPY_ARRAY_PARAM(p) memcpy(dev->p, target->p, sizeof(dev->p))
  632.     COPY_PARAM(width);
  633.     COPY_PARAM(height);
  634.     COPY_ARRAY_PARAM(MediaSize);
  635.     COPY_ARRAY_PARAM(ImagingBBox);
  636.     COPY_PARAM(ImagingBBox_set);
  637.     COPY_ARRAY_PARAM(HWResolution);
  638.     COPY_ARRAY_PARAM(MarginsHWResolution);
  639.     COPY_ARRAY_PARAM(Margins);
  640.     COPY_ARRAY_PARAM(HWMargins);
  641.     COPY_PARAM(PageCount);
  642. #undef COPY_ARRAY_PARAM
  643.     gx_device_copy_color_params(dev, target);
  644. }
  645.  
  646. #undef COPY_PARAM
  647.  
  648. /*
  649.  * Parse the output file name for a device, recognizing "-" and "|command",
  650.  * and also detecting and validating any %nnd format for inserting the
  651.  * page count.  If a format is present, store a pointer to its last
  652.  * character in *pfmt, otherwise store 0 there.  Note that an empty name
  653.  * is currently allowed.
  654.  */
  655. int
  656. gx_parse_output_file_name(gs_parsed_file_name_t *pfn, const char **pfmt,
  657.               const char *fname, uint fnlen)
  658. {
  659.     int code;
  660.     bool have_format = false, field = 0;
  661.     int width[2], int_width = sizeof(int) * 3, w = 0;
  662.     uint i;
  663.  
  664.     *pfmt = 0;
  665.     if (fnlen == 0) {        /* allow null name */
  666.     pfn->memory = 0;
  667.     pfn->iodev = NULL;
  668.     pfn->fname = 0;        /* irrelevant since length = 0 */
  669.     pfn->len = 0;
  670.     return 0;
  671.     }
  672.     /*
  673.      * If the file name begins with a %, it might be either an IODevice
  674.      * or a %nnd format.  Distinguish the two by searching for a second %.
  675.      */
  676.     if (fname[0] == '%' && !memchr(fname + 1, '%', fnlen - 1)) {
  677.     /* This is a file name starting with a format. */
  678.     pfn->memory = 0;
  679.     pfn->iodev = NULL;
  680.     pfn->fname = fname;
  681.     pfn->len = fnlen;
  682.     } else {
  683.     code = gs_parse_file_name(pfn, fname, fnlen);
  684.     if (code < 0)
  685.         return code;
  686.     }
  687.     if (!pfn->iodev) {
  688.     if (!strcmp(pfn->fname, "-")) {
  689.         pfn->iodev = gs_findiodevice((const byte *)"%stdout", 7);
  690.         pfn->fname = NULL;
  691.     } else if (pfn->fname[0] == '|') {
  692.         pfn->iodev = gs_findiodevice((const byte *)"%pipe", 5);
  693.         pfn->fname++, pfn->len--;
  694.     } else
  695.         pfn->iodev = iodev_default;
  696.     if (!pfn->iodev)
  697.         return_error(gs_error_undefinedfilename);
  698.     }
  699.     if (!pfn->fname)
  700.     return 0;
  701.     /* Scan the file name for a format string, and validate it if present. */
  702.     width[0] = width[1] = 0;
  703.     for (i = 0; i < pfn->len; ++i)
  704.     if (pfn->fname[i] == '%') {
  705.         if (i + 1 < pfn->len && pfn->fname[i + 1] == '%')
  706.         continue;
  707.         if (have_format)    /* more than one % */
  708.         return_error(gs_error_rangecheck);
  709.         have_format = true;
  710.     sw:
  711.         if (++i == pfn->len)
  712.         return_error(gs_error_rangecheck);
  713.         switch (pfn->fname[i]) {
  714.         case 'l':
  715.             int_width = sizeof(long) * 3;
  716.         case ' ': case '#': case '+': case '-':
  717.             goto sw;
  718.         case '.':
  719.             if (field)
  720.             return_error(gs_error_rangecheck);
  721.             field = 1;
  722.             continue;
  723.         case '0': case '1': case '2': case '3': case '4':
  724.         case '5': case '6': case '7': case '8': case '9':
  725.             width[field] = width[field] * 10 + pfn->fname[i] - '0';
  726.             goto sw;
  727.         case 'd': case 'i': case 'u': case 'o': case 'x': case 'X':
  728.             *pfmt = &pfn->fname[i];
  729.             continue;
  730.         default:
  731.             return_error(gs_error_rangecheck);
  732.         }
  733.     }
  734.     if (have_format) {
  735.     /* Calculate a conservative maximum width. */
  736.     w = max(width[0], width[1]);
  737.     w = max(w, int_width) + 5;
  738.     }
  739.     if (strlen(pfn->iodev->dname) + pfn->len + w >= gp_file_name_sizeof)
  740.     return_error(gs_error_rangecheck);
  741.     return 0;
  742. }
  743.  
  744. /* Open the output file for a device. */
  745. int
  746. gx_device_open_output_file(const gx_device * dev, char *fname,
  747.                bool binary, bool positionable, FILE ** pfile)
  748. {
  749.     gs_parsed_file_name_t parsed;
  750.     const char *fmt;
  751.     char pfname[gp_file_name_sizeof];
  752.     int code = gx_parse_output_file_name(&parsed, &fmt, fname, strlen(fname));
  753.  
  754.     if (code < 0)
  755.     return code;
  756.     if (parsed.iodev && !strcmp(parsed.iodev->dname, "%stdout%")) {
  757.     if (parsed.fname)
  758.         return_error(gs_error_undefinedfilename);
  759.     *pfile = gs_stdout;
  760.     /* Force stdout to binary. */
  761.     return gp_setmode_binary(*pfile, true);
  762.     }
  763.     if (fmt) {
  764.     long count1 = dev->PageCount + 1;
  765.  
  766.     while (*fmt != 'l' && *fmt != '%')
  767.         --fmt;
  768.     if (*fmt == 'l')
  769.         sprintf(pfname, parsed.fname, count1);
  770.     else
  771.         sprintf(pfname, parsed.fname, (int)count1);
  772.     parsed.fname = pfname;
  773.     parsed.len = strlen(parsed.fname);
  774.     }
  775.     if (positionable || (parsed.iodev && parsed.iodev != iodev_default)) {
  776.     char fmode[4];
  777.  
  778.     if (!parsed.fname)
  779.         return_error(gs_error_undefinedfilename);
  780.     strcpy(fmode, gp_fmode_wb);
  781.     if (positionable)
  782.         strcat(fmode, "+");
  783.     code = parsed.iodev->procs.fopen(parsed.iodev, parsed.fname, fmode,
  784.                      pfile, NULL, 0);
  785.     if (*pfile)
  786.         return 0;
  787.     }
  788.     *pfile = gp_open_printer((fmt ? pfname : fname), binary);
  789.     if (*pfile)
  790.     return 0;
  791.     return_error(gs_error_invalidfileaccess);
  792. }
  793.  
  794. /* Close the output file for a device. */
  795. int
  796. gx_device_close_output_file(const gx_device * dev, const char *fname,
  797.                 FILE *file)
  798. {
  799.     gs_parsed_file_name_t parsed;
  800.     const char *fmt;
  801.     int code = gx_parse_output_file_name(&parsed, &fmt, fname, strlen(fname));
  802.  
  803.     if (code < 0)
  804.     return code;
  805.     if (parsed.iodev) {
  806.     if (!strcmp(parsed.iodev->dname, "%stdout%"))
  807.         return 0;
  808.     /* NOTE: fname is unsubstituted if the name has any %nnd formats. */
  809.     if (parsed.iodev != iodev_default)
  810.         return parsed.iodev->procs.fclose(parsed.iodev, file);
  811.     }
  812.     gp_close_printer(file, (parsed.fname ? parsed.fname : fname));
  813.     return 0;
  814. }
  815.